package teamdamn.view;

/**
 * The Refresh Panel, used by Admin users to refresh the local data with remote data.
 * @author jrsto674
 * @author joelhun
 */
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;

import javax.swing.JLabel;

import teamdamn.controller.DBIO;
import teamdamn.model.ResponseList;

import java.awt.Font;
import java.io.File;
public class RefreshPanel extends JPanel {

	private JButton btnRefreshData;
	
	private ResponseList myCurrentResponseList;
	
	private final String updatedFilePath;
	
	private GUI myGUI;
	
	/**
	 * Create the panel.
	 * @author ???
	 * @edited joelhun
	 */
	public RefreshPanel(GUI theGUI, ResponseList initialResponseList) {
	    File myPath = new File("repository");
        updatedFilePath = myPath.getAbsolutePath() + DBIO.PathSeparator() + "PostUpdateQuestions.txt";
		myGUI = theGUI;
		myCurrentResponseList = initialResponseList;
		setLayout(new GridLayout(0, 1, 0, 0));
		
		JPanel panel = new JPanel();
		add(panel);
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.columnWidths = new int[]{50, 100, 50, 0};
		gbl_panel.rowHeights = new int[]{50, 30, 70, 50, 0};
		gbl_panel.columnWeights = new double[]{1.0, 0.0, 1.0, Double.MIN_VALUE};
		gbl_panel.rowWeights = new double[]{1.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
		panel.setLayout(gbl_panel);
		
		JLabel lblNewrLabel = new JLabel("Click to sync local data with remote server.");
		GridBagConstraints gbc_lblNewrLabel = new GridBagConstraints();
		gbc_lblNewrLabel.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewrLabel.gridx = 1;
		gbc_lblNewrLabel.gridy = 1;
		panel.add(lblNewrLabel, gbc_lblNewrLabel);
		
		btnRefreshData = new JButton("Refresh Data");
		btnRefreshData.setFont(new Font("Tahoma", Font.BOLD, 20));
		GridBagConstraints gbc_btnRefreshData = new GridBagConstraints();
		gbc_btnRefreshData.fill = GridBagConstraints.BOTH;
		gbc_btnRefreshData.insets = new Insets(0, 0, 5, 5);
		gbc_btnRefreshData.gridx = 1;
		gbc_btnRefreshData.gridy = 2;
		panel.add(btnRefreshData, gbc_btnRefreshData);
		
		
		
		btnRefreshData.addActionListener(new ActionListener() {
			/* (non-Javadoc)
			 * @author kjudoy
			 * @author joelhun
			 */
			public void actionPerformed(ActionEvent e) {
				
				JFrame frame = new JFrame("MSG Refresh");
				int choice = JOptionPane.showConfirmDialog(myGUI, "Are you sure you want to refresh the data? Local data may be lost forever.", 
															"Refresh Data?", JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);
				if (choice == JOptionPane.YES_OPTION) {
					updateResponseList();
				} else JOptionPane.showMessageDialog(myGUI, "Data not updated!");
			}
		});

	}
	
	/**
	 * Updates the response list. This is a dummy implementation
	 * @author joelhun
	 */
	private void updateResponseList() {
		myCurrentResponseList.updateResponses(updatedFilePath);
	}

}
